﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    nutrientDAO dao;
    Nutrient nutrient;

    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet userRole = new DataSet();
        userRole = (DataSet) Session["USER"];
        nutrient = new Nutrient();
        dao = new nutrientDAO();
        if (IsPostBack == false)
        {
            load_data();
            if (userRole.Tables[0].Rows[0]["Role"].ToString() == "admin")
            {
                gotoAddNew.Visible = true;
                GridView1.Enabled = true;
            }
            else
            {
                gotoAddNew.Visible = false;
                GridView1.Enabled = false;
            }
        }
    }
   
    public void load_data()
    {
        DataSet ds = new DataSet();
        ds = dao.get();
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }


    protected void Button1_Click1(object sender, EventArgs e)
    {
        Response.Redirect("addNutrient.aspx");
    }


    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        String nutrientID = GridView1.DataKeys[e.RowIndex].Value.ToString();
        nutrient.Id = nutrientID;
        dao.delete(nutrient);
        Response.Write("<script>alert('Delete Success!')</script>");
        load_data();
    }
  

    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        String id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
        nutrient.Id = id;
        DataSet nutrientUpdate = new DataSet();
        nutrientUpdate = dao.getNutrient(nutrient);
        Session.Add("NUTRI", nutrientUpdate);
        Response.Redirect("updateNutrient.aspx");
    }
}